package com.web.ShoppingDemo.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Component;

/**
 * 实现短息的发送功能
 * 
 * @author THT
 * 
 * 
 * 
 */
@Component("messageUtil")
public class MessageUtilImpl implements MessageUtil {
	/**
	 * 发送短信的方法
	 * @param number
	 * @param id
	 */
	public void sendMessage(String number, String id) {
		// 1.打开浏览器
		HttpClient client = new HttpClient();
		// 2.创建请求的方式 get/post
		PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn/");
		// 3.设置请求的参数信息
		post.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=utf-8");
		post.setParameter("Uid", "foder");
		post.setParameter("Key", "62ed89aa3d81f6f184a4");
		post.setParameter("smsMob", number);
		post.setParameter("smsText", "订单:"+id+"完成");
		try {
			// 4.提交请求
			int code = client.executeMethod(post);
			System.out.println("http返回的状态码:" + code);
			// 5.获取服务器返回的数据信息
			String result = post.getResponseBodyAsString();
			System.out.println("短信发送的结果为:" + result);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally{
			post.releaseConnection(); 
		}
	}
	/*
	 * public static void main(String[] args)throws Exception {
	 * 
	 * HttpClient client = new HttpClient(); PostMethod post = new
	 * PostMethod("http://gbk.sms.webchinese.cn");
	 * post.addRequestHeader("Content-Type"
	 * ,"application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
	 * NameValuePair[] data ={ new NameValuePair("Uid", "本站用户名") ,new
	 * NameValuePair("Key", "接口安全秘钥"),new NameValuePair("smsMob","手机号码") ,new
	 * NameValuePair("smsText","验证码：8888")}; post.setRequestBody(data);
	 * 
	 * client.executeMethod(post);
	 * 
	 * Header[] headers = post.getResponseHeaders(); int statusCode =
	 * post.getStatusCode(); System.out.println("statusCode:"+statusCode);
	 * for(Header h : headers) { System.out.println(h.toString()); } String
	 * result = new String(post.getResponseBodyAsString().getBytes("gbk"));
	 * System.out.println(result); //打印返回消息状态
	 * 
	 * post.releaseConnection(); 
	 * }
	 */
}
